package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {

    @Value("${upload.path}")
    private String uploadPath;
//    @PostConstruct
//    public void postConstruct(){
//        System.out.println();
//        System.out.println("uploadPath:"+uploadPath);
//        System.out.println();
//    }

    @RequestMapping(value = "/hi")
    @ResponseBody
    public Object sayHi(){

        return "hih7";
    }
    @GetMapping("/get-type")
    @ResponseBody
    public String getType(){
        return "你好getMaping";
    }
    @ResponseBody
    @RequestMapping("/reg")
    public Object reg2(@RequestBody User user){
        return user;
    }
    @RequestMapping("/file")
    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
        String string1=file.getOriginalFilename();
        System.out.println(string1);
        int count=string1.lastIndexOf('.');
        String str=string1.substring(count+1,string1.length());
        String str1=UUID.randomUUID().toString();
        String string=str1+'.'+str;
        String string2=uploadPath+string;
        file.transferTo(new File(string2));
        return "上传成功";
    }
}
